//
//  PTNavigationBar.m
//  platformDemo
//
//  Created by Owen on 2020/6/9.
//  Copyright © 2020 Owen. All rights reserved.
//

#import "PTNavigationBar.h"

@implementation PTNavigationBar

- (instancetype)init{

    self = [super init];
    if (self) {
        self.frame = CGRectMake(0,
                                0,
                                WIDTH,
                                NavigationBarHeight + StatusBarHeight);
        self.backgroundColor = [UIColor clearColor];

        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0,
                                                                0,
                                                                WIDTH - NavigationBarHeight*2,
                                                                20)];
        [self addSubview:view];
        
        self.bgView = [[UIView alloc] initWithFrame:CGRectMake(0,
                                                               StatusBarHeight,
                                                               WIDTH,
                                                               NavigationBarHeight)];
        self.bgView.backgroundColor = [UIColor clearColor];
        [self addSubview:self.bgView];
        
        
        
//        UITapGestureRecognizer *doubleRecognizer;
//        doubleRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTapFrom)];
//        doubleRecognizer.numberOfTapsRequired = 2; /*双击*/
//        [view addGestureRecognizer:doubleRecognizer];
    }
    return self;
}

- (void)layoutSubviews{

    [super layoutSubviews];
    self.titleLabel = [[UILabel alloc] init];
    self.titleLabel.backgroundColor = [UIColor clearColor];
    self.titleLabel.text = self.titleContent;
    self.titleLabel.font = [UIFont systemFontOfSize:17 weight:UIFontWeightRegular];
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    [self.titleLabel sizeToFit];
    if (self.titleLabel.frame.size.width > ([UIScreen mainScreen].bounds.size.width - NavigationBarHeight*2)) {

        self.titleLabel.font = SSMediumFont(15);
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        self.titleLabel.numberOfLines = 0;
        self.titleLabel.frame = CGRectMake(WIDTH/2.0 - (WIDTH - NavigationBarHeight*2)/2.0,
                                      NavigationBarHeight/2.0 - (self.titleLabel.frame.size.height*2)/2.0,
                                      WIDTH - NavigationBarHeight*2,
                                      self.titleLabel.frame.size.height*2);

    }else{

        self.titleLabel.frame = CGRectMake(WIDTH/2.0 - (WIDTH - NavigationBarHeight*2)/2.0,
                                      NavigationBarHeight/2.0 - self.titleLabel.frame.size.height/2.0 ,
                                      WIDTH - NavigationBarHeight*2,
                                      self.titleLabel.frame.size.height);

    }
    
    if (!self.titleColor) {
        self.titleLabel.textColor = [UIColor blackColor];
    }else{
        self.titleLabel.textColor = self.titleColor;
    }
    
    [self.bgView addSubview:self.titleLabel];
    self.bgView.backgroundColor = self.backgroundColor;
}


- (void) setLeftBarItem{
   
    UIButton *leftBtn = [[UIButton alloc] initWithFrame:CGRectMake(0,
                                                                   0,
                                                                   NavigationBarHeight,
                                                                   NavigationBarHeight)];
    [leftBtn setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(dismissCurrentVController) forControlEvents:UIControlEventTouchUpInside];
    [self.bgView addSubview:leftBtn];

}

#pragma mark - dismissCurrentVC

- (void) dismissCurrentVController{

    if ([self.delegate respondsToSelector:@selector(didSelectedLeftBarItem)]) {
        [self.delegate didSelectedLeftBarItem];
    }
}

@end
